package android.medicalcloud.com.yiapp.yilian.ui.ac;

import android.medicalcloud.com.yiapp.R;
import android.medicalcloud.com.yiapp.base.activity.BaseActivity;
import android.medicalcloud.com.yiapp.base.cach.PreferceManager;
import android.medicalcloud.com.yiapp.base.net.lisener.ViewNetCallBack;
import android.medicalcloud.com.yiapp.base.utils.Logger;
import android.medicalcloud.com.yiapp.base.utils.StringTools;
import android.medicalcloud.com.yiapp.yilian.configer.contance.Constance;
import android.medicalcloud.com.yiapp.yilian.configer.enums.HttpConfig;
import android.medicalcloud.com.yiapp.yilian.logic.PushController;
import android.medicalcloud.com.yiapp.yilian.logic.UserController;
import android.medicalcloud.com.yiapp.yilian.model.ImConfigModel;
import android.medicalcloud.com.yiapp.yilian.utils.android.IntentTool;
import android.medicalcloud.com.yiapp.yilian.utils.android.JsonTool;
import android.medicalcloud.com.yiapp.yilian.utils.android.ToastTool;
import android.medicalcloud.com.yiapp.yilian.utils.im.PushUtil;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.tencent.TIMCallBack;
import com.tencent.TIMManager;
import com.tencent.connect.UserInfo;
import com.tencent.qcloud.presentation.business.LoginBusiness;
import com.tencent.qcloud.presentation.event.MessageEvent;

import java.io.Serializable;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by wanggenlin on 17-5-18.
 */
public class LoginActivity extends BaseActivity implements ViewNetCallBack, TIMCallBack {

    EditText userNameEt;
    EditText pasEt;

    @Override
    protected void initView() {
        userNameEt = (EditText) findViewById(R.id.userNameEt);
        pasEt = (EditText) findViewById(R.id.pasEt);
        String templeName = UserController.getInstance().getTempleUserName();
        if (!StringTools.isNullOrEmpty(templeName)){
            userNameEt.setText(templeName);
        }
        findViewById(R.id.loginBt).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name = userNameEt.getText().toString();
                String paw = pasEt.getText().toString();
                if (StringTools.isNullOrEmpty(name) || StringTools.isNullOrEmpty(paw)) {
                    Toast.makeText(LoginActivity.this, "用户名密码不能为空", Toast.LENGTH_LONG).show();
                    return;
                }
                UserController.getInstance().login(LoginActivity.this, name, paw);

            }
        });
        findViewById(R.id.passwordTv).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                IntentTool.findPassword(LoginActivity.this);
            }
        });

        pasEt.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == KeyEvent.ACTION_DOWN || actionId == EditorInfo.IME_ACTION_SEND) {
                    String name = userNameEt.getText().toString();
                    String paw = pasEt.getText().toString();
                    if (StringTools.isNullOrEmpty(name) || StringTools.isNullOrEmpty(paw)) {
                        Toast.makeText(LoginActivity.this, "用户名密码不能为空", Toast.LENGTH_LONG).show();
                        return false;
                    }
                    UserController.getInstance().login(LoginActivity.this, name, paw);
                }
                return true;
            }
        });

    }

    @Override
    protected void initEvent() {

    }

    @Override
    protected void initData() {


    }


    @Override
    protected int getLayoutId() {
        return R.layout.ac_login;
    }

    @Override
    public void onConnectStart(Object o) {

    }

    @Override
    public void onConnectEnd() {

    }

    @Override
    public void onFail(Exception e, Object o, String reslut) {

    }

    @Override
    public void onData(Serializable result, int flag, boolean fromNet, Object o) {
        if (flag == HttpConfig.Login.getType()) {

            UserController.getInstance().saveTempleUserName(userNameEt.getText().toString());
            if (JsonTool.getString(o.toString(), "status").equals("1")) {
                return;
            }
            String token = JsonTool.getString(o.toString(), "token");
            Logger.e(o.toString() + "======ui");
            UserController.getInstance().saveToken(token);
            UserController.getInstance().loginIm(this);
//            IntentTool.startMain(LoginActivity.this);
//            finish();

        }
        if (flag == HttpConfig.imSig.getType()){
            Logger.e("from_identifier = "+PreferceManager.getInsance().getValueBYkey("from_identifier"));
            Logger.e("from_usersig = "+PreferceManager.getInsance().getValueBYkey("from_usersig"));
            ImConfigModel imModel = (ImConfigModel)result;
            PreferceManager.getInsance().saveValueBYkey("from_identifier",imModel.getFrom_identifier());
            PreferceManager.getInsance().saveValueBYkey("from_usersig",imModel.getFrom_usersig());
            PreferceManager.getInsance().saveValueBYkey("to_identifier",imModel.getTo_identifier());
            PreferceManager.getInsance().saveValueBYkey("to_usersig",imModel.getTo_usersig());
            LoginBusiness.loginIm(PreferceManager.getInsance().getValueBYkey("from_identifier"),
                    PreferceManager.getInsance().getValueBYkey("from_usersig"),
                    Constance.APPID, LoginActivity.this);
        }


    }

    @Override
    public void onSuccess() {
//初始化程序后台后消息推送
        PushUtil.getInstance();
        //初始化消息监听
        MessageEvent.getInstance();
        PushController.getInstance().bind();
        Logger.e("imsdk env " + TIMManager.getInstance().getEnv());
        IntentTool.startMain(this);
        finish();
    }

    @Override
    public void onError(int i, String s) {
        Logger.e("login error : code " + i + " " + s);
        UserController.getInstance().cleanToken();
        switch (i) {
            case 6208:
                //离线状态下被其他终端踢下线
                ToastTool.show("您的账号在其他地方登录，请重新登录");
                break;
            case 6200:
                ToastTool.show("登录失败，当前无网络");
                IntentTool.login(LoginActivity.this);
                finish();
                break;
            default:
                ToastTool.show("IM登录失败，请稍后重试");

                break;
        }
    }
}
